﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using SF.DBUtility;
using SF.Common;
namespace SF.DAL
{
    /// <summary>
    /// 数据访问类:bs_good_order
    /// </summary>
    public partial class bs_good_order
    {
        public bs_good_order()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from bs_good_order");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string where)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from bs_good_order");
            if (where != "")
            {
                strSql.Append(" where " + where);
            }
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public long Add(SF.Model.bs_good_order model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into bs_good_order(");
            strSql.Append("busnieseNum,appNum,templatesNum,orderNum,userNum,totalPrice,createTime,payStatus,payWay,payTime,expressStatus,expressName,expressNum,expressTime,receiveInfo,userConfirmStatus,sysConfirmStatus,confirmTime,status,remark)");
            strSql.Append(" values (");
            strSql.Append("@busnieseNum,@appNum,@templatesNum,@orderNum,@userNum,@totalPrice,@createTime,@payStatus,@payWay,@payTime,@expressStatus,@expressName,@expressNum,@expressTime,@receiveInfo,@userConfirmStatus,@sysConfirmStatus,@confirmTime,@status,@remark)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					new SqlParameter("@appNum", SqlDbType.NVarChar,50),
					new SqlParameter("@templatesNum", SqlDbType.NVarChar,50),
					new SqlParameter("@orderNum", SqlDbType.NVarChar,50),
					new SqlParameter("@userNum", SqlDbType.NVarChar,50),
					new SqlParameter("@totalPrice", SqlDbType.Decimal,9),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@payStatus", SqlDbType.NVarChar,50),
					new SqlParameter("@payWay", SqlDbType.NVarChar,50),
					new SqlParameter("@payTime", SqlDbType.DateTime),
					new SqlParameter("@expressStatus", SqlDbType.NVarChar,50),
					new SqlParameter("@expressName", SqlDbType.NVarChar,50),
					new SqlParameter("@expressNum", SqlDbType.NVarChar,50),
					new SqlParameter("@expressTime", SqlDbType.DateTime),
					new SqlParameter("@receiveInfo", SqlDbType.NVarChar,-1),
					new SqlParameter("@userConfirmStatus", SqlDbType.NVarChar,50),
					new SqlParameter("@sysConfirmStatus", SqlDbType.NVarChar,50),
					new SqlParameter("@confirmTime", SqlDbType.DateTime),
					new SqlParameter("@status", SqlDbType.NVarChar,50),
					new SqlParameter("@remark", SqlDbType.NVarChar,-1)};
            parameters[0].Value = model.busnieseNum;
            parameters[1].Value = model.appNum;
            parameters[2].Value = model.templatesNum;
            parameters[3].Value = model.orderNum;
            parameters[4].Value = model.userNum;
            parameters[5].Value = model.totalPrice;
            parameters[6].Value = model.createTime;
            parameters[7].Value = model.payStatus;
            parameters[8].Value = model.payWay;
            parameters[9].Value = model.payTime;
            parameters[10].Value = model.expressStatus;
            parameters[11].Value = model.expressName;
            parameters[12].Value = model.expressNum;
            parameters[13].Value = model.expressTime;
            parameters[14].Value = model.receiveInfo;
            parameters[15].Value = model.userConfirmStatus;
            parameters[16].Value = model.sysConfirmStatus;
            parameters[17].Value = model.confirmTime;
            parameters[18].Value = model.status;
            parameters[19].Value = model.remark;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SF.Model.bs_good_order model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update bs_good_order set ");
            strSql.Append("busnieseNum=@busnieseNum,");
            strSql.Append("appNum=@appNum,");
            strSql.Append("templatesNum=@templatesNum,");
            strSql.Append("orderNum=@orderNum,");
            strSql.Append("userNum=@userNum,");
            strSql.Append("totalPrice=@totalPrice,");
            strSql.Append("createTime=@createTime,");
            strSql.Append("payStatus=@payStatus,");
            strSql.Append("payWay=@payWay,");
            strSql.Append("payTime=@payTime,");
            strSql.Append("expressStatus=@expressStatus,");
            strSql.Append("expressName=@expressName,");
            strSql.Append("expressNum=@expressNum,");
            strSql.Append("expressTime=@expressTime,");
            strSql.Append("receiveInfo=@receiveInfo,");
            strSql.Append("userConfirmStatus=@userConfirmStatus,");
            strSql.Append("sysConfirmStatus=@sysConfirmStatus,");
            strSql.Append("confirmTime=@confirmTime,");
            strSql.Append("status=@status,");
            strSql.Append("remark=@remark");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					new SqlParameter("@appNum", SqlDbType.NVarChar,50),
					new SqlParameter("@templatesNum", SqlDbType.NVarChar,50),
					new SqlParameter("@orderNum", SqlDbType.NVarChar,50),
					new SqlParameter("@userNum", SqlDbType.NVarChar,50),
					new SqlParameter("@totalPrice", SqlDbType.Decimal,9),
					new SqlParameter("@createTime", SqlDbType.DateTime),
					new SqlParameter("@payStatus", SqlDbType.NVarChar,50),
					new SqlParameter("@payWay", SqlDbType.NVarChar,50),
					new SqlParameter("@payTime", SqlDbType.DateTime),
					new SqlParameter("@expressStatus", SqlDbType.NVarChar,50),
					new SqlParameter("@expressName", SqlDbType.NVarChar,50),
					new SqlParameter("@expressNum", SqlDbType.NVarChar,50),
					new SqlParameter("@expressTime", SqlDbType.DateTime),
					new SqlParameter("@receiveInfo", SqlDbType.NVarChar,-1),
					new SqlParameter("@userConfirmStatus", SqlDbType.NVarChar,50),
					new SqlParameter("@sysConfirmStatus", SqlDbType.NVarChar,50),
					new SqlParameter("@confirmTime", SqlDbType.DateTime),
					new SqlParameter("@status", SqlDbType.NVarChar,50),
					new SqlParameter("@remark", SqlDbType.NVarChar,-1),
					new SqlParameter("@id", SqlDbType.BigInt,8)};
            parameters[0].Value = model.busnieseNum;
            parameters[1].Value = model.appNum;
            parameters[2].Value = model.templatesNum;
            parameters[3].Value = model.orderNum;
            parameters[4].Value = model.userNum;
            parameters[5].Value = model.totalPrice;
            parameters[6].Value = model.createTime;
            parameters[7].Value = model.payStatus;
            parameters[8].Value = model.payWay;
            parameters[9].Value = model.payTime;
            parameters[10].Value = model.expressStatus;
            parameters[11].Value = model.expressName;
            parameters[12].Value = model.expressNum;
            parameters[13].Value = model.expressTime;
            parameters[14].Value = model.receiveInfo;
            parameters[15].Value = model.userConfirmStatus;
            parameters[16].Value = model.sysConfirmStatus;
            parameters[17].Value = model.confirmTime;
            parameters[18].Value = model.status;
            parameters[19].Value = model.remark;
            parameters[20].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from bs_good_order ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string where)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from bs_good_order ");
            if (where != "")
            {
                strSql.Append(" where " + where);
            }
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from bs_good_order ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SF.Model.bs_good_order GetModel(long id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,busnieseNum,appNum,templatesNum,orderNum,userNum,totalPrice,createTime,payStatus,payWay,payTime,expressStatus,expressName,expressNum,expressTime,receiveInfo,userConfirmStatus,sysConfirmStatus,confirmTime,status,remark from bs_good_order ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
            parameters[0].Value = id;

            SF.Model.bs_good_order model = new SF.Model.bs_good_order();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SF.Model.bs_good_order GetModel(string where)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,busnieseNum,appNum,templatesNum,orderNum,userNum,totalPrice,createTime,payStatus,payWay,payTime,expressStatus,expressName,expressNum,expressTime,receiveInfo,userConfirmStatus,sysConfirmStatus,confirmTime,status,remark from bs_good_order ");
            if (where != "")
            {
                strSql.Append(" where " + where);
            }
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SF.Model.bs_good_order DataRowToModel(DataRow row)
        {
            SF.Model.bs_good_order model = new SF.Model.bs_good_order();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = long.Parse(row["id"].ToString());
                }
                if (row["busnieseNum"] != null)
                {
                    model.busnieseNum = row["busnieseNum"].ToString();
                }
                if (row["appNum"] != null)
                {
                    model.appNum = row["appNum"].ToString();
                }

                if (row["templatesNum"] != null)
                {
                    model.templatesNum = row["templatesNum"].ToString();
                }
                if (row["orderNum"] != null)
                {
                    model.orderNum = row["orderNum"].ToString();
                }
                if (row["userNum"] != null && row["userNum"].ToString() != "")
                {
                    model.userNum = row["userNum"].ToString();
                }
                if (row["totalPrice"] != null && row["totalPrice"].ToString() != "")
                {
                    model.totalPrice = decimal.Parse(row["totalPrice"].ToString());
                }
                if (row["createTime"] != null && row["createTime"].ToString() != "")
                {
                    model.createTime = DateTime.Parse(row["createTime"].ToString());
                }
                if (row["payStatus"] != null)
                {
                    model.payStatus = row["payStatus"].ToString();
                }
                if (row["payWay"] != null)
                {
                    model.payWay = row["payWay"].ToString();
                }
                if (row["payTime"] != null && row["payTime"].ToString() != "")
                {
                    model.payTime = DateTime.Parse(row["payTime"].ToString());
                }
                if (row["expressStatus"] != null)
                {
                    model.expressStatus = row["expressStatus"].ToString();
                }
                if (row["expressName"] != null)
                {
                    model.expressName = row["expressName"].ToString();
                }
                if (row["expressNum"] != null)
                {
                    model.expressNum = row["expressNum"].ToString();
                }
                if (row["expressTime"] != null && row["expressTime"].ToString() != "")
                {
                    model.expressTime = DateTime.Parse(row["expressTime"].ToString());
                }
                if (row["receiveInfo"] != null)
                {
                    model.receiveInfo = row["receiveInfo"].ToString();
                }
                if (row["userConfirmStatus"] != null)
                {
                    model.userConfirmStatus = row["userConfirmStatus"].ToString();
                }
                if (row["sysConfirmStatus"] != null)
                {
                    model.sysConfirmStatus = row["sysConfirmStatus"].ToString();
                }
                if (row["confirmTime"] != null && row["confirmTime"].ToString() != "")
                {
                    model.confirmTime = DateTime.Parse(row["confirmTime"].ToString());
                }
                if (row["status"] != null)
                {
                    model.status = row["status"].ToString();
                }
                if (row["remark"] != null)
                {
                    model.remark = row["remark"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,busnieseNum,appNum,templatesNum,orderNum,userNum,totalPrice,createTime,payStatus,payWay,payTime,expressStatus,expressName,expressNum,expressTime,receiveInfo,userConfirmStatus,sysConfirmStatus,confirmTime,status,remark ");
            strSql.Append(" FROM bs_good_order ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,busnieseNum,appNum,templatesNum,orderNum,userNum,totalPrice,createTime,payStatus,payWay,payTime,expressStatus,expressName,expressNum,expressTime,receiveInfo,userConfirmStatus,sysConfirmStatus,confirmTime,status,remark ");
            strSql.Append(" FROM bs_good_order ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM bs_good_order ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from bs_good_order T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * FROM bs_good_order");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(strSql.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, strSql.ToString(), filedOrder));
        }

        #endregion  BasicMethod
        #region  ExtensionMethod
        /// <summary>
        /// 计算某个字段的总和值
        /// </summary>
        /// <param name="field">字段</param>
        /// <param name="where">条件</param>
        /// <returns></returns>
        public DataSet GetSumField(string field, string where)
        {
            String sql = @"select SUM({0}) as {0} from  bs_good_order {1}";
            if (String.IsNullOrEmpty(field))
                return null;
            else
            {
                sql = String.Format(sql, field, String.IsNullOrEmpty(where) ? "" : " where " + where);

            }
            return DbHelperSQL.Query(sql);
        }

        /// <summary>
        /// 获得查询分页数据
        /// </summary>
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount, string isOur)
        {
            String sql = @"select * from bs_good_order {0}";
            sql = string.Format(sql, strWhere.Trim() != "" ? " where " + strWhere : "");
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(sql)));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, sql, filedOrder));
        }
        #endregion  ExtensionMethod
    }
}

